/*
 * Copyright (c) 2009, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "tssp_internal.h"
#include "minunit.h"
#include "tssp_dvb_dr_00.h"

#define PREF                            "tssp_dr_application"
#define TO_PROFILE_COUNT(data)          ((data)[0] / 5)
#define TO_PROFILE(data, x)             TSSP_TO_UINT16(&(data)[1 + 5 * x])
#define TO_VERSION_MAJOR(data, x)       ((data)[1 + 5 * x + 2])
#define TO_VERSION_MINOR(data, x)       ((data)[1 + 5 * x + 3])
#define TO_VERSION_MICRO(data, x)       ((data)[1 + 5 * x + 4])
#define TO_SERVICE_BOUND_FLAG(data, n)  (0x80 & (data)[1 + 5 * n])
#define TO_VISIBILITY(data, n)          ((0x60 & (data)[1 + 5 * n]) >> 5)
#define TO_APP_PRIORITY(data, n)        ((data)[1 + 5 * n + 1])
#define TO_PROTOCOL_LABEL(data, n)      (&(data)[1 + 5 * n + 2])
#define TO_LABEL_LENGTH(length, n)      (length - (1 + 5 * n + 2))

static uint8_t data0[] = {
    5 * 1, // application_profiles_length (8)

    0x00, 0x01, // application_profile (16)
    1, // version.major (8)
    2, // version.minor (8)
    3, // version.micro (8)

    // service_bound_flag (1), visibility (2), reserved (5)
    (1 << 7) | (3 << 5) | 0,

    0xA3, // application_priority (8)

    'a', 'b', 'c', // transport_protocol_label (8 * N)
};

static uint8_t data1[] = {
    5 * 0, // application_profiles_length (8)

    // application_profile (16)
    // version.major (8)
    // version.minor (8)
    // version.micro (8)

    // service_bound_flag (1), visibility (2), reserved (5)
    (1 << 7) | (3 << 5) | 0,

    0xFF, // application_priority (8)

    'x', // transport_protocol_label (8 * N)
};

static uint8_t data2[] = {
    5 * 2, // application_profiles_length (8)

    0x00, 0x01, // application_profile (16)
    1, // version.major (8)
    2, // version.minor (8)
    3, // version.micro (8)

    0x10, 0xA3, // application_profile (16)
    0xFF, // version.major (8)
    0x0F, // version.minor (8)
    0xF0, // version.micro (8)

    // service_bound_flag (1), visibility (2), reserved (5)
    (0 << 7) | (0 << 5) | 0x1F,

    0x3A, // application_priority (8)

    // transport_protocol_label (8 * N)
};

static struct {
    int length;
    uint8_t *data;
} testdata[] = {
    {TSSP_NUMELEM(data0), data0},
    {TSSP_NUMELEM(data1), data1},
    {TSSP_NUMELEM(data2), data2},
};

static
const char *test_tssp_dr_application_parse()
{
    int i;
    int j;
    int n;
    int ret;
    struct tssp_dr_application dest;
    uint8_t *data;
    struct tssp_descriptor src = {
        .tag = 0x00,
        .length = 0xFF,
        .data = NULL,
        .decoded = NULL,
        .destroy = free,
    };

    for (i = 0; i < TSSP_NUMELEM(testdata); i++) {
        src.length = testdata[i].length;
        src.data = data = testdata[i].data;

        ret = tssp_dr_application_parse(&dest, &src);
        mu_assert(PREF "parse", TSSP_STATUS_OK == ret);
        mu_assert(PREF "parse - profile_count",
                  dest.profile_count == TO_PROFILE_COUNT(data));
 
        n = dest.profile_count;
        for (j = 0; j < n; j++) {
            mu_assert(PREF "parse - profile",
                      dest.profile[j].profile == TO_PROFILE(data, j));
            mu_assert(PREF "parse - version.major",
                      dest.profile[j].version.major == TO_VERSION_MAJOR(data, j));
            mu_assert(PREF "parse - version.minor",
                      dest.profile[j].version.minor == TO_VERSION_MINOR(data, j));
            mu_assert(PREF "parse - version.micro",
                      dest.profile[j].version.micro == TO_VERSION_MICRO(data, j));
        }
 
        if (dest.service_bound_flag) {
            mu_assert(PREF "parse - service_bound_flag",
                      TO_SERVICE_BOUND_FLAG(data, n) != 0);
        } else {
            mu_assert(PREF "parse - service_bound_flag",
                      TO_SERVICE_BOUND_FLAG(data, n) == 0);
        }
 
        mu_assert(PREF "parse - visibility",
                  dest.visibility == TO_VISIBILITY(data, n));
        mu_assert(PREF "parse - priority",
                  dest.priority == TO_APP_PRIORITY(data, n));
        mu_assert(PREF "parse - transport_protocol_length",
                  dest.transport_protocol_length == TO_LABEL_LENGTH(src.length, n));
        mu_assert(PREF "parse - transport_protocol_label",
                  0 == memcmp(dest.transport_protocol_label,
                              TO_PROTOCOL_LABEL(data, n),
                              dest.transport_protocol_length));
    }

    return NULL;
}

static
const char *test_tssp_dr_application_create()
{
    int i;
    struct tssp_dr_application *result;
    struct tssp_descriptor src = {
        .tag = 0x00,
        .length = 0xFF,
        .data = NULL,
        .decoded = NULL,
        .destroy = free,
    };

    for (i = 0; i < TSSP_NUMELEM(testdata); i++) {
        src.length = testdata[i].length;
        src.data = testdata[i].data;
        result = tssp_dr_application_create(&src);
        mu_assert(PREF "create", result != NULL);
        tssp_dr_application_destroy(result);
    }

    return NULL;
}

static
const char *test_tssp_dr_application_error()
{
    int i;
    int ret;
    struct tssp_dr_application *tmp;
    struct tssp_dr_application dest;
    uint8_t data[] = {
        51 * 5,
        5 * 1,
        5 * 1 + 1,
    };
    struct {
        int expected;
        struct tssp_descriptor src;
    } errdata[] = {
        {
            TSSP_STATUS_FAILED,
            {
                .tag = 0x01,
            },
        },
        {
            TSSP_STATUS_FAILED,
            {
                .tag = 0x00,
                .length = 2,
            },
        },
        {
            TSSP_STATUS_FAILED,
            {
                .tag = 0x00,
                .length = 3,
                .data = &data[0],
            },
        },
        {
            TSSP_STATUS_FAILED,
            {
                .tag = 0x00,
                .length = 7,
                .data = &data[1],
            },
        },
        {
            TSSP_STATUS_FAILED,
            {
                .tag = 0x00,
                .length = 9,
                .data = &data[2],
            },
        },
    };

    for (i = 0; i < TSSP_NUMELEM(errdata); i++) {
        ret = tssp_dr_application_parse(&dest, &errdata[i].src);
        mu_assert(PREF "parser_error", errdata[i].expected == ret);
    }
    for (i = 0; i < TSSP_NUMELEM(errdata); i++) {
        tmp = tssp_dr_application_create(&errdata[i].src);
        mu_assert(PREF "create_error", NULL == tmp);
    }

    return NULL;
}

const char *test_tssp_dvb_dr_00()
{
    mu_run_test(test_tssp_dr_application_parse);
    mu_run_test(test_tssp_dr_application_create);
    mu_run_test(test_tssp_dr_application_error);
    return NULL;
}

